package g

import (
	"context"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var (
	mongoClient *mongo.Client
	collection  *mongo.Collection
	ctx         context.Context
)

func InitMongoClient() (err error) {
	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
	mongoClient, err = mongo.Connect(
		ctx, options.Client().ApplyURI("mongodb://root:fIQx8EyQfgfLEHjg@111.229.198.48:27119/"))
	if err != nil {
		return err
	}

	collection = mongoClient.Database("nCoV").Collection("DXYArea")
	return nil
}

func Disconnect() error {
	if mongoClient != nil {
		return mongoClient.Disconnect(ctx)
	}

	return nil
}

func Collection() *mongo.Collection {
	if mongoClient == nil {
		InitMongoClient()
	}

	return collection
}
